package com.pctx.rbac.service.impl;

import com.pctx.common.util.DateTimeUtil;
import com.pctx.common.util.UuidUtil;
import com.pctx.rbac.dao.PermissionMapper;
import com.pctx.rbac.pojo.Permission;
import com.pctx.rbac.pojo.PermissionExample;
import com.pctx.rbac.service.PermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by chason on 2018/2/24.11:28
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getByCompanyUuid(String companyId) throws Exception {
        if (StringUtils.isEmpty(companyId))
            return null;
        PermissionExample example = new PermissionExample();
        example.setOrderByClause("sorting ,create_time desc");
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyUuidEqualTo(companyId).andStatusNotEqualTo(Permission.STATUS_DEL);

        return permissionMapper.selectByExample(example);
    }

    @Override
    public List<Permission> getByCompanyUuid4NotOwned(String companyId) throws Exception {
        if (StringUtils.isEmpty(companyId))
            return null;
        return permissionMapper.getByCompanyUuid4NotOwned(companyId);
    }

    @Override
    public boolean addCompanyPermission(String companyUuid, Integer permissionId) throws Exception {
        if (StringUtils.isEmpty(companyUuid) || null == permissionId)
            return false;

        Permission parent = getById(permissionId);
        if (null == parent)
            return false;

        Permission permisson = getByCompanyUuidAndParentId(companyUuid, permissionId);
        if (null != permisson)
            return false;

        parent.setUuid(UuidUtil.randomUUID());
        parent.setParentId(parent.getId());
        parent.setId(null);
        parent.setCompanyUuid(companyUuid);
        parent.setCreateTime(DateTimeUtil.nowDate());
        parent.setUpdateTime(DateTimeUtil.nowDate());

        return add(parent);
    }

    @Override
    public Permission getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return null;

        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid).andStatusNotEqualTo(Permission.STATUS_DEL);

        List<Permission> list = permissionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public Permission getById(Integer id) throws Exception {
        if (null == id)
            return null;

        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id).andStatusNotEqualTo(Permission.STATUS_DEL);

        List<Permission> list = permissionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public Permission getByCompanyUuidAndParentId(String companyUuid, Integer parentId) throws Exception {
        if (StringUtils.isEmpty(companyUuid) || null == parentId)
            return null;

        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyUuidEqualTo(companyUuid).andParentIdEqualTo(parentId).andStatusNotEqualTo(Permission.STATUS_DEL);

        List<Permission> list = permissionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public boolean delByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return false;

        Permission permission = getByUuid(uuid);
        if (null == permission)
            return false;

        permission.setStatus(Permission.STATUS_DEL);

        return update(permission);
    }

    @Override
    public boolean update(Permission permission)throws Exception{
        if (null == permission)
            return false;

        return permissionMapper.updateByPrimaryKeySelective(permission) > 0;
    }

    @Override
    public List<Permission> getByRoleUuid(String roleUuid) throws Exception {
        if (StringUtils.isEmpty(roleUuid))
            return null;

        return permissionMapper.getByRoleUuid(roleUuid);
    }

    @Override
    public boolean add(Permission permission) throws Exception {
        if (null == permission)
            return false;

        return permissionMapper.insertSelective(permission) > 0;
    }
}
